package com.flexfiltering.view.controls.hierarchicalBrowserClasses
{
import flash.display.DisplayObject;

import mx.controls.List;
import mx.core.EdgeMetrics;
import mx.core.IUIComponent;
import mx.core.mx_internal;

use namespace mx_internal;

[Style(name="titleBarOverlapsBorder",type="boolean",defaultValue="true")]

/**
 * 	This extends list to add support for a titleBar above the list.  The title
 * 	bar can be any class that implements IUIComponent
 */ 
public class TitleList extends List
{
	private var titleBarChanged:Boolean;
	private var oldTitleBar:IUIComponent;
	
	private var _titleBar:IUIComponent;
	
	public function get titleBar () : IUIComponent
	{
		return _titleBar;
	}
	
	public function set titleBar ( val:IUIComponent ) : void
	{
		if ( val == _titleBar )
			return;
		
		oldTitleBar = _titleBar;
		_titleBar = val;
		titleBarChanged = true;
		
		invalidateProperties();
		invalidateDisplayList();
		
		// andy.mcintosh [10/19/2007] -- don't know why i can't call this method
		// i'm using the mx_internal namespace.
//		invalidateViewMetricsAndPadding();
	}
	
	
	override protected function commitProperties():void
	{
		super.commitProperties();
		
		if ( titleBarChanged )
		{
			titleBarChanged = false;
			
			if ( oldTitleBar != null )
			{
				removeChild(DisplayObject(oldTitleBar));
			}
			
			if ( titleBar != null )
			{
				addChild(DisplayObject(_titleBar));
			}
		}	
	}
	
	override protected function updateDisplayList(w:Number, h:Number):void
	{
		super.updateDisplayList(w,h);
		
		if ( _titleBar )
		{
			if ( getStyle('titleBarOverlapsBorder') == false )
			{
				var bm:EdgeMetrics = super.borderMetrics;
				
				_titleBar.x = bm.left;
				_titleBar.y = bm.top;
				_titleBar.setActualSize(w-bm.left-bm.right,_titleBar.getExplicitOrMeasuredHeight());
			}
			else
			{
				_titleBar.x = 0;
				_titleBar.y = 0;
				_titleBar.setActualSize(w,_titleBar.getExplicitOrMeasuredHeight());
			}
		}	
	}

	override public function get borderMetrics():EdgeMetrics
	{
		var bm:EdgeMetrics = super.borderMetrics.clone();
		
		if(titleBar)
		{
			bm.top = titleBar.getExplicitOrMeasuredHeight();
		}
		
		return bm;
	}


}
}